<?php
/**
 * Created by PhpStorm.
 * User: tingyugetc
 * Date: 16/12/22
 * Time: 上午12:26
 */

namespace App\Repositories;


use App\Models\Category;

class CategoryRepository
{
    use BaseRepository;

    protected $model;
    public function __construct(Category $category) {
        $this->model = $category;
    }

    /**
     * @param $pageSize
     * @return mixed
     */
    public function listsByPage($pageSize) {
        return $this->model->orderBy('id', 'desc')->paginate($pageSize);
    }

    public function lists() {
        return Category::all();
//        return $this->model->orderBy('id', 'desc')->get();
    }

    public function listsByIdKey() {
        $categoriesList = $this->lists();
        $categories = [];
        foreach ($categoriesList as $category) {
            $categories[$category->id] = $category->cate_name;
        }
        return $categories;
    }

    public function store($input)
    {
        return $this->savePost($this->model, $input);
    }

    public function update($cateId, $input) {
        $category = $this->model->find($cateId);
        return $this->savePost($category, $input);
    }

    public function savePost(Category $category, $input) {
        $category->fill($input->all());

        return $category->save();
    }

    public function find($cateId) {
        return $this->model->find($cateId);
    }

}